<?php
class sample_reference_model extends MY_Model {
	var $model = 'sample_reference';
	
	function GetJSON()
	{
		$ls_sql	= "	SELECT	* 
					FROM	sample_reference 
					WHERE	(ref_name LIKE '%".post('keyword')."%')
					ORDER BY ".(post('sort')=='' ? 'ref_key' : post('sort'))." ".(post('order')=='' ? 'asc' : post('order'));
					
		return json_encode( $this->paging($ls_sql) );
	}
		
	function Add($arr_data){
		$this->db->insert('sample_reference', $arr_data);
		
		$this->AuditTrail(	'sample_reference',	// Table
							'Create',	// Action
							''			// Primary Key
							);
	}
	
	function Update($arr_data, $arr_rule){
		$this->db->update('sample_reference', $arr_data, $arr_rule);
		
		$this->AuditTrail(	'sample_reference',		// Table
							'Update',				// Action
							json_encode($arr_rule)	// Primary Key
							);
	}
	
	function GetSingle($as_roleid){
		$result = $this->db->get_where('sample_reference', array('ref_key' => $as_roleid));
		if($result->num_rows() == 1){
			return $result->row_array();
		}else{
			return NULL;
		}
	}
	
	function GetSingleView($as_roleid)
	{
		$result = $this->db->get_where('sample_reference', array('ref_key' => $as_roleid));
		if($result->num_rows() == 1){
			return $result->row_array();
		}else{
			return NULL;
		}
	}

	function DeleteSingle($arr_rule)
	{
		try {
			if(	intval($this->get_data("SELECT COUNT(1) FROM sample_master WHERE ref_key = ".$arr_rule['ref_key']))>0)
			{
				return $this->error_delete.' [id: '.$arr_rule['ref_key']."]<br />";
			}else{
				$this->db->delete('sample_reference', $arr_rule);
				
				$this->AuditTrail(	'sample_reference',		// Table
									'Delete',				// Action
									json_encode($arr_rule)	// Primary Key
									);
				return '';
			}
		} catch(Exception $e) {
			return $e->getMessage();
		}
	}
}
?>